package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class AlbumInfoApiController {

    @Autowired
    private AlbumInfoService albumInfoService;

    @Operation(summary = "获取当前用户专辑列表")
    @GetMapping("/findUserAllAlbumList")
    public Result findUserAllAlbumList() {
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        List<AlbumInfo> albumInfoList = albumInfoService.findUserAllAlbumList(userId);
        return Result.ok(albumInfoList);
    }

    /**
     * 根据Id查询专辑
     * @param id
     * @return
     */
    @Operation(summary = "根据Id查询专辑")
    @GetMapping("getAlbumInfo/{id}")
    public Result getAlbumInfo(@PathVariable Long id) {
        AlbumInfo albumInfo = albumInfoService.getAlbumInfoById(id);
        return Result.ok(albumInfo);
    }

    /**
     * 删除专辑
     * @param id
     * @return
     */
    @Operation(summary = "删除专辑")
    @DeleteMapping("/removeAlbumInfo/{id}")
    public Result removeAlbumInfo(@PathVariable Long id) {
        albumInfoService.removeAlbumInfo(id);
        return Result.ok();
    }

    /**
     * 专辑列表分页查询
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "专辑列表分页查询")
    @PostMapping("/findUserAlbumPage/{page}/{limit}")
    public Result findUserAlbumPage(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    @RequestBody AlbumInfoQuery albumInfoQuery) {
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        // 赋值
        albumInfoQuery.setUserId(userId);
        // 使用mybatis-plus实现条件分页查询
        Page<AlbumListVo> albumListVoPage = new Page<>(page, limit);
        // 调用服务层方法
        IPage<AlbumListVo> albumListVoIPage = albumInfoService.getUserAlbumPage(albumListVoPage, albumInfoQuery);
        return Result.ok(albumListVoIPage);
    }

    /**
     * 保存专辑
     *
     * @param albumInfoVo
     * @return
     */
    @Operation(summary = "保存专辑")
    @PostMapping("/saveAlbumInfo")
    private Result saveAlbumInfo(@RequestBody AlbumInfoVo albumInfoVo) {
        //  获取用户Id
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();

        albumInfoService.saveAlbumInfo(albumInfoVo, userId);
        return Result.ok();
    }

    @Operation(summary = "修改专辑")
    @PutMapping("/updateAlbumInfo/{id}")
    public Result updateAlbumInfo(@PathVariable Long id, @RequestBody AlbumInfoVo albumInfoVo){
        albumInfoService.updateAlbumInfo(id, albumInfoVo);
        return Result.ok();
    }

}

